<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="创建时间" prop="gmtCreate">
      <el-input v-model="dataForm.gmtCreate" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="gmtModified">
      <el-input v-model="dataForm.gmtModified" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="code" prop="productCode">
      <el-input v-model="dataForm.productCode" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="用户名" prop="productUserName">
      <el-input v-model="dataForm.productUserName" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="渠道" prop="productChannel">
      <el-input v-model="dataForm.productChannel" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="尺寸（宽）" prop="productImageWidth">
      <el-input v-model="dataForm.productImageWidth" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="尺寸（高）" prop="productImageHeight">
      <el-input v-model="dataForm.productImageHeight" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="客单价" prop="productUnitPrice">
      <el-input v-model="dataForm.productUnitPrice" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="productRemark">
      <el-input v-model="dataForm.productRemark" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="productStatus">
      <el-input v-model="dataForm.productStatus" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="生产图" prop="productEndUrl">
      <el-input v-model="dataForm.productEndUrl" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="psd图" prop="productEndPsdUrl">
      <el-input v-model="dataForm.productEndPsdUrl" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="扩展" prop="extInfo">
      <el-input v-model="dataForm.extInfo" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="环境" prop="env">
      <el-input v-model="dataForm.env" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          gmtCreate: '',
          gmtModified: '',
          productCode: '',
          productUserName: '',
          productChannel: '',
          productImageWidth: '',
          productImageHeight: '',
          productUnitPrice: '',
          productRemark: '',
          productStatus: '',
          productEndUrl: '',
          productEndPsdUrl: '',
          extInfo: '',
          env: ''
        },
        dataRule: {
          gmtCreate: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          gmtModified: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          productCode: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          productUserName: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          productChannel: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          productImageWidth: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          productImageHeight: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          productUnitPrice: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          productRemark: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          productStatus: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          productEndUrl: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          productEndPsdUrl: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          extInfo: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          env: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/product/product/queryWormholeGalaxyProduct`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.gmtCreate = data.wmhGalaxyProduct.gmtCreate
                this.dataForm.gmtModified = data.wmhGalaxyProduct.gmtModified
                this.dataForm.productCode = data.wmhGalaxyProduct.productCode
                this.dataForm.productUserName = data.wmhGalaxyProduct.productUserName
                this.dataForm.productChannel = data.wmhGalaxyProduct.productChannel
                this.dataForm.productImageWidth = data.wmhGalaxyProduct.productImageWidth
                this.dataForm.productImageHeight = data.wmhGalaxyProduct.productImageHeight
                this.dataForm.productUnitPrice = data.wmhGalaxyProduct.productUnitPrice
                this.dataForm.productRemark = data.wmhGalaxyProduct.productRemark
                this.dataForm.productStatus = data.wmhGalaxyProduct.productStatus
                this.dataForm.productEndUrl = data.wmhGalaxyProduct.productEndUrl
                this.dataForm.productEndPsdUrl = data.wmhGalaxyProduct.productEndPsdUrl
                this.dataForm.extInfo = data.wmhGalaxyProduct.extInfo
                this.dataForm.env = data.wmhGalaxyProduct.env
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/product/product/${!this.dataForm.id ? 'createWormholeGalaxyProduct' : 'createWormholeGalaxyProduct'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'gmtCreate': this.dataForm.gmtCreate,
                'gmtModified': this.dataForm.gmtModified,
                'productCode': this.dataForm.productCode,
                'productUserName': this.dataForm.productUserName,
                'productChannel': this.dataForm.productChannel,
                'productImageWidth': this.dataForm.productImageWidth,
                'productImageHeight': this.dataForm.productImageHeight,
                'productUnitPrice': this.dataForm.productUnitPrice,
                'productRemark': this.dataForm.productRemark,
                'productStatus': this.dataForm.productStatus,
                'productEndUrl': this.dataForm.productEndUrl,
                'productEndPsdUrl': this.dataForm.productEndPsdUrl,
                'extInfo': this.dataForm.extInfo,
                'env': this.dataForm.env
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
